package org.duanyu.shopping_cart_customer_api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.duanyu.shopping_common.pojo.CartGoods;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.CartService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/cart")
public class CartController {

    @DubboReference
    private CartService cartService;

    /**
     * 查询购物车商品
     * @param userId 用户id
     * @return 购物车列表
     */
    @GetMapping("/findCartList")
    public BaseResult<List<CartGoods>> findCartList(@RequestHeader Long userId){
        List<CartGoods> cartList = cartService.findCartList(userId);
        return BaseResult.success(cartList);
    }

    /**
     * 新增商品到购物车
     * @param userId 令牌中携带的用户id
     * @param cartGoods 购物车商品
     * @return 操作结果
     */
    @PostMapping("/addCart")
    public BaseResult addCart(@RequestHeader Long userId,@RequestBody CartGoods cartGoods){
        cartService.addCart(userId,cartGoods);
        return BaseResult.success();
    }

    /**
     * 修改购物车中的商品数量
     * @param userId 令牌中携带的用户Id
     * @param goodsId 商品id
     * @param num 修改后的数量
     * @return 操作结果
     */
    @GetMapping("/handleCart")
    public BaseResult handleCart(@RequestHeader Long userId,Long goodsId,Integer num){
        cartService.handleCart(userId, goodsId, num);
        return BaseResult.success();
    }

    /**
     * 删除购物车商品
     * @param userId 令牌中携带的用户id
     * @param goodsId 商品id
     * @return 操作结果
     */
    @DeleteMapping("/deleteCart")
    public BaseResult deleteCart(@RequestHeader Long userId,Long goodsId){
        cartService.deleteCartOption(userId, goodsId);
        return BaseResult.success();
    }

}
